//
//  LGTPostCollectionStepVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/23.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import WisdomHUD

class LGTPostCollectionStepVC: LGTBaseVC {
    var textView:LGTInputTextView?
    var index:Int = 0 //在视图上的索引位置
    var clickInfoBlock:ClickInfoBlock? //保存回掉
    var path:IndexPath?
    private var _info:Dictionary<String, Any>?
     //初始化信息
    var info:Dictionary<String, Any>?{
        get{
            return _info
        }set{
            _info = newValue
            
            self.path = newValue?["path"] as? IndexPath
            
        }
    }
    
//    func setInitUI(){
//        if self.info != nil{
//            self.textView?.textView.text = self.info?["stepText"] as? String ?? ""
//            self.textView?.textView.text = "23131"
//        }
//    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setUI() {
        super.setUI()
        self.view.backgroundColor = BlackAlphaColor
        let backView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(10))
        self.view.addSubview(backView)
        backView.isUserInteractionEnabled = true
        backView.backgroundColor = UIColor.white
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(self.view)
            make.width.equalTo(SizeFit(303))
            make.height.equalTo(SizeFit(328))
        }
        
        let titleLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: 0, text: "收集信息")
        backView.addSubview(titleLabel)
        
        titleLabel.font = UIFont.boldSystemFont(ofSize: SizeFitFloat(18))
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(backView)
            make.height.equalTo(SizeFit(18))
            make.top.equalTo(SizeFit(22))
        }
        
        let clostBtn = LGTViewCreateUtil.share.createLGTButton(image: "close_icon", selected: "close_icon", dW: SizeFitFloat(10), dH: SizeFitFloat(10))
        backView.addSubview(clostBtn)
        clostBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(SizeFit(13))
            make.top.equalTo(SizeFit(23))
            make.right.equalTo(-SizeFit(24))
        }
        
        let descLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "步骤说明*")
        backView.addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(27))
            make.top.equalTo(SizeFit(59))
            make.height.equalTo(SizeFitFloat(15))
        }
        
        let imageView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(3))
        imageView.isUserInteractionEnabled = true
        imageView.backgroundColor = ViewBackColor
        backView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(backView)
            make.height.equalTo(SizeFitFloat(102))
            make.width.equalTo(SizeFitFloat(252))
            make.top.equalTo(SizeFit(84))
        }
        
        self.textView = LGTViewCreateUtil.share.createInputTextView(placehoder: "适用于收集手机号、用户名、姓名、ID等输入内容，详细说明你要收集什么。", fontSize: SizeFitFloat(12), limitCout: 200)
        imageView.addSubview(self.textView!)
        self.textView?.textView.backgroundColor = UIColor.clear
        self.textView?.textView.isScrollEnabled = true
        self.textView?.showCount = true
        self.textView?.backgroundColor = UIColor.clear
        self.textView!.snp.makeConstraints({ (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(SizeFit(10))
            make.width.equalTo(SizeFitFloat(230))
            make.bottom.equalTo(SizeFit(-18))
        })
        
        let sureButton:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "确认添加", font: Font_Normal_15, color: UIColor.white, selectedColor: UIColor.white, corner:SizeFitFloat(20), backColor: YellowColor)
        backView.addSubview(sureButton)
        sureButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(SizeFit(40))
            make.width.equalTo(SizeFit(252))
            make.top.equalTo(SizeFit(221))
        }
        
        let cancelBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "取消", font: Font_Normal_15, color: YellowColor, selectedColor: YellowColor, corner:SizeFitFloat(20), backColor: UIColor.clear)
        backView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(SizeFit(40))
            make.width.equalTo(SizeFit(252))
            make.top.equalTo(sureButton.snp_bottomMargin).offset(SizeFit(10))
        }
        
        cancelBtn.addTarget(self, action: #selector(closeAction), for: UIControl.Event.touchUpInside)
        clostBtn.addTarget(self, action: #selector(closeAction), for: UIControl.Event.touchUpInside)
        sureButton.addTarget(self, action: #selector(sureAction), for: UIControl.Event.touchUpInside)
        
        if self.info != nil{
            self.textView?.text = self.info?["stepText"] as? String ?? ""
        }
        
    }
    
    @objc func addImageAction(){
        print("addImage")
    }
    
    @objc func closeAction(){
        print("closeAction")
        self.dismiss(animated: false, completion: nil)
    }
    
    @objc func sureAction(){
        print("sureAction")
        
        if self.textView?.textView.text.count == 0{
            WisdomHUD.showText(text: "请输入步骤说明")
            return
        }
        
        //步骤添加
        var info:Dictionary<String, Any> = ["index": self.index,
                                            "stepText": self.textView!.textView.text,
                                            "collection": true]
        if self.path != nil{
            info["path"] = self.path!
        }
        if self.clickInfoBlock != nil{
            self.clickInfoBlock!(self.index, info)
        }
        self.dismiss(animated: false, completion: nil)
    }
}
